package cn.cttic.link.service.impl;

import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.link.api.domain.bo.BaseThirdInterfaceBo;
import cn.cttic.link.api.domain.vo.BaseThirdInterfaceVo;
import cn.cttic.link.api.domain.BaseThirdInterface;
import cn.cttic.link.mapper.BaseThirdInterfaceMapper;
import cn.cttic.link.service.IBaseThirdInterfaceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 请求历史Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@RequiredArgsConstructor
@Service
public class BaseThirdInterfaceServiceImpl implements IBaseThirdInterfaceService {

    private final BaseThirdInterfaceMapper baseMapper;

    /**
     * 查询请求历史
     *
     * @param id 主键
     * @return 请求历史
     */
    @Override
    public BaseThirdInterfaceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询请求历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 请求历史分页列表
     */
    @Override
    public TableDataInfo<BaseThirdInterfaceVo> queryPageList(BaseThirdInterfaceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseThirdInterface> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(
            BaseThirdInterface::getCreateTime
        );
        Page<BaseThirdInterfaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的请求历史列表
     *
     * @param bo 查询条件
     * @return 请求历史列表
     */
    @Override
    public List<BaseThirdInterfaceVo> queryList(BaseThirdInterfaceBo bo) {
        LambdaQueryWrapper<BaseThirdInterface> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseThirdInterface> buildQueryWrapper(BaseThirdInterfaceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseThirdInterface> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInterfaceType()), BaseThirdInterface::getInterfaceType, bo.getInterfaceType());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessUrl()), BaseThirdInterface::getAccessUrl, bo.getAccessUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestType()), BaseThirdInterface::getRequestType, bo.getRequestType());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestParam()), BaseThirdInterface::getRequestParam, bo.getRequestParam());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestBody()), BaseThirdInterface::getRequestBody, bo.getRequestBody());
        lqw.eq(StringUtils.isNotBlank(bo.getResponseBody()), BaseThirdInterface::getResponseBody, bo.getResponseBody());
        lqw.eq(StringUtils.isNotBlank(bo.getResult()), BaseThirdInterface::getResult, bo.getResult());
        return lqw;
    }

    /**
     * 新增请求历史
     *
     * @param bo 请求历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseThirdInterfaceBo bo) {
        BaseThirdInterface add = MapstructUtils.convert(bo, BaseThirdInterface.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改请求历史
     *
     * @param bo 请求历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseThirdInterfaceBo bo) {
        BaseThirdInterface update = MapstructUtils.convert(bo, BaseThirdInterface.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseThirdInterface entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除请求历史信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
